﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.MonThiService;
namespace eTMS.Controllers{
public class MonThiControllerBase : CommonController{
	public MonThiControllerBase()
		: base()
	{	
		this.dao = new MonThiServiceClient();
	}
	~MonThiControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private MonThiEntity oMonThi;	
	public MonThiEntity MonThiObject{
		get{
			return oMonThi;
		}
		set{
			oMonThi=value;
		}
	}
	protected MonThiServiceClient dao;
	public DataTable Table{
		get{
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oMonThi==null)return false;
		bool result=true;
		try{
			oMonThi = dao.SaveRet(oMonThi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListMonThi.Add(oMonThi);
		}
		return result;
	}
	public bool Update(){
		if(oMonThi==null)return false;
		return dao.Update(oMonThi);
	}
	public bool Delete(){
		if(oMonThi==null)return false;
		bool retvalue = dao.Delete(oMonThi);
		if(retvalue){			
			_ListMonThi.Remove(oMonThi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 MonThi_ID){
		bool retvalue = dao.DeleteByID(MonThi_ID);		
		if(retvalue){
			MonThiEntity oDeleted=FindMonThi(MonThi_ID);
			if(oDeleted!=null){
				_ListMonThi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oMonThi = new MonThiEntity();
	}
    //Copy data from data row in grid to object
    public MonThiEntity MapDataRowToObject(DataRow row){
		MonThiEntity o = new MonThiEntity();
		 if(!row.IsNull("LanThi"))
			o.LanThi=ConvertByte(row["LanThi"]);
		 if(!row.IsNull("NgayThi"))
			o.NgayThi=ConvertDateTime(row["NgayThi"]);
		 if(!row.IsNull("TrangThai"))
			o.TrangThai=ConvertByte(row["TrangThai"]);
		 if(!row.IsNull("KyThi_ID"))
			o.KyThi_ID=(Int32)(row["KyThi_ID"]);
		 if(!row.IsNull("MonHoc_ID"))
			o.MonHoc_ID=(Int32)(row["MonHoc_ID"]);
		 if(!row.IsNull("MonThi_ID"))
		    o.MonThi_ID=(Int32)(row["MonThi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(MonThiEntity o,DataRow row){
		row["LanThi"]=o.LanThi;
		row["NgayThi"]=o.NgayThi;
		row["TrangThai"]=o.TrangThai;
		row["KyThi_ID"]=o.KyThi_ID;
		row["MonHoc_ID"]=o.MonHoc_ID;
		row["MonThi_ID"]=o.MonThi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.MonThiObject,row);
    }
	public void LoadObject(Int32 MonThi_ID){
		MonThiEntity oLoad=FindMonThi(MonThi_ID);
		if(oLoad==null){
			oMonThi=this.LoadMonThi(MonThi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oMonThi.IsNullMonThi_ID;
			if(!bNotIsNull){
				this.ListMonThi.Add(oMonThi);
			}
		}
		else{
			oMonThi=oLoad;
		}
	}
	#region CachedList MonThi
	private static List<MonThiEntity> _ListMonThi=new List<MonThiEntity>();
    public List<MonThiEntity>ListMonThi{
		get{
			return _ListMonThi;
		}
	}
	public static List<MonThiEntity>ListMonThiStatic{
		get{
			return _ListMonThi;
		}
	}
	public MonThiEntity FindMonThi(Int32 MonThi_ID){
		MonThiEntity oResult=this.ListMonThi.Find(delegate(MonThiEntity oFind){
			bool result=true; 
			result&=oFind.MonThi_ID.Equals(MonThi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllMonThi(bool clearCache){
		if(_ListMonThi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		MonThiServiceClient dao = new MonThiServiceClient();
		_ListMonThi=new List<MonThiEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public MonThiEntity LoadMonThi(Int32 MonThi_ID){			
		MonThiEntity o=dao.SelectOne(MonThi_ID);		
		return o;
	}
	#endregion
}
}
